using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Reflection;

namespace TR0217.ControlEx
{
    [ToolboxBitmap(typeof(EyedropColorPicker), "Resources.eyedropper.bmp")]
	public class EyedropColorPicker : Control
	{
		public event EventHandler SelectedColorChanged;

		Bitmap	_snapshot;
		Bitmap	_icon;
		Color	_selectedColor;
		float  _zoom = 4;
		public int Zoom
		{
			get { return (int)_zoom; }
			set 
			{ 
				_zoom = value;
				RecalcSnapshotSize();
			}
		}
		public Color SelectedColor
		{
			get { return _selectedColor; }
			set 
			{
				if (_selectedColor == value)
					return;
			}
		}
		public EyedropColorPicker()
		{
			this.DoubleBuffered = true;
			_icon = new Bitmap(typeof(EyedropColorPicker), "Resources.eyedropper.bmp");
			_icon.MakeTransparent(Color.Magenta);
		}
		RectangleF ImageRect
		{
			get
			{
				return Util.Rect(ClientRectangle);
			}
		}
		protected override void OnResize(EventArgs e)
		{
			base.OnResize(e);
			RecalcSnapshotSize();
		}
		void RecalcSnapshotSize()
		{
			if (_snapshot != null)
				_snapshot.Dispose();
			RectangleF r = ImageRect;
			int w = (int)(Math.Floor(r.Width / Zoom));
			int h = (int)(Math.Floor(r.Height / Zoom));
			_snapshot = new Bitmap(w, h);
		}
		void GetSnapshot()
		{
			Point p = Control.MousePosition;
			p.X -= _snapshot.Width / 2;
			p.Y -= _snapshot.Height / 2;

			using (Graphics dc = Graphics.FromImage(_snapshot))
			{
				dc.CopyFromScreen(p, new Point(0,0), _snapshot.Size);
				Refresh(); //Invalidate();
				
				PointF center = Util.Center(new RectangleF(0, 0, _snapshot.Size.Width, _snapshot.Size.Height));
				Color c = _snapshot.GetPixel((int)Math.Round(center.X), (int)Math.Round(center.Y));
				if (c != _selectedColor)
				{
					_selectedColor = c;
					if (SelectedColorChanged != null)
						SelectedColorChanged(this, null);
				}
			}
		}
		protected override void OnPaint(PaintEventArgs e)
		{
			base.OnPaint(e);
			Rectangle rr = ClientRectangle;

			if (_snapshot != null)
			{
				e.Graphics.InterpolationMode = InterpolationMode.NearestNeighbor;
				RectangleF r = RectangleF.Empty;
				r.X = 4;
				r.Y = 4;
                r.Width = this.Width - 6;
                r.Height = this.Height - 6;
				e.Graphics.DrawImage(_snapshot, r);

				if (iscapturing)
				{
					PointF center = Util.Center(r);
					Rectangle centerrect = new Rectangle(Util.Point(center), new Size(0, 0));
					centerrect.X -= ((int)Zoom / 2 - 1);
					centerrect.Y -= ((int)Zoom / 2 - 1);
					centerrect.Width = (int)Zoom;
					centerrect.Height = (int)Zoom;
					e.Graphics.DrawRectangle(Pens.Black, centerrect);
				}
				else
				{
					int offset = 3;
					e.Graphics.DrawImage(_icon, offset, offset);
				}
			}
            rr.Inflate(-1,-1);
			Util.DrawFrame(e.Graphics, rr, 6, Color.CadetBlue);
		}
		bool iscapturing = false;
		protected override void OnMouseDown(MouseEventArgs e)
		{
			base.OnMouseDown(e);
			if ((e.Button & MouseButtons.Left)== MouseButtons.Left)
			{
				Cursor = Cursors.Cross;
				iscapturing = true;
				Invalidate();
			}
		}
		protected override void OnMouseMove(MouseEventArgs e)
		{
			base.OnMouseMove(e);
			if ((e.Button & MouseButtons.Left)== MouseButtons.Left)
				GetSnapshot();
		}
		protected override void OnMouseUp(MouseEventArgs e)
		{
			base.OnMouseUp(e);
			{
				Cursor = Cursors.Arrow;
				iscapturing = false;
				Invalidate();
			}
		}

        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing);
            _icon.Dispose();
            _snapshot.Dispose();
        }
	}
}
